package practise;

public class Test {
    public static void main(String[] args) {

    }
}
class Solution {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(root == p || root == q) {
            return root;
        }
        TreeNode leftNode = lowestCommonAncestor(root.left,p,q);
        TreeNode rightNode = lowestCommonAncestor(root.right,p,q);
        if(leftNode!=null && rightNode!=null) {
            return root;
        } else if(leftNode!=null) {
            return leftNode;
        } else{
            return rightNode;
        }
    }
}